package com.eqdd.yiqidian.fragment;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.eqdd.annotation.api.DealCodeInjector;
import com.eqdd.annotation.datasave.DealCodeClass;
import com.eqdd.yiqidian.MineFragmentCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.EnterBean;
import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.bean.rebuild.TipBean;
import com.eqdd.yiqidian.bean.rebuild.UserAuthInfoResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.rxbus2.RxBus;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.eqdd.yiqidian.ui.MineAuActivity;
import com.eqdd.yiqidian.ui.gongneng.daka.DakaActivity;
import com.eqdd.yiqidian.ui.mine.UserDocumentActivity;
import com.eqdd.yiqidian.ui.mine.UserErweimaActivity;
import com.eqdd.yiqidian.ui.mine.UserInfoActivity;
import com.eqdd.yiqidian.ui.mine.kanban.MyKanbanActivity;
import com.eqdd.yiqidian.ui.mine.pizhun.MyPizhunActivity;
import com.eqdd.yiqidian.ui.mine.qiye.QiyeActivity;
import com.eqdd.yiqidian.ui.mine.renwu.RenwuActivity;
import com.eqdd.yiqidian.ui.mine.setting.XiTongSettingActivity;
import com.eqdd.yiqidian.ui.mine.shenqing.MyShenqingActivity;
import com.eqdd.yiqidian.ui.mine.shenqing.ShowMemoActivity;
import com.eqdd.yiqidian.ui.mine.wo.CollectionActivity;
import com.eqdd.yiqidian.ui.mine.wo.WoActivity;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import io.rong.imkit.widget.AlterDialogFragment;

/**
 * Created by lvzhihao on 17-4-15.
 */

public class MineFragment extends BaseFragment {

    private MineFragmentCustom dataBinding;

    public List<EnterBean> enterBeans = new ArrayList<>();
    private User user;
    private TextView tvName;
    private ImageView ivHead;
    private SlimAdapterEx slimAdapterEx;
    private TextView tvNum;
    private Object dealCodeInjector;

    @DealCodeClass(classes = {MyShenqingActivity.class})
    public void updateShengqing(int num) {
        TipBean dataItem = (TipBean) slimAdapterEx.getDataItem(1);
        dataItem.setNum(num);
        slimAdapterEx.notifyItemChanged(1);
    }

    @DealCodeClass(classes = {MyPizhunActivity.class})
    public void updatePizhun(int num) {
        TipBean dataItem = (TipBean) slimAdapterEx.getDataItem(2);
        dataItem.setNum(num);
        slimAdapterEx.notifyItemChanged(2);
    }

    @DealCodeClass(classes = {RenwuActivity.class})
    public void updateRenwu(int num) {
        TipBean dataItem = (TipBean) slimAdapterEx.getDataItem(4);
        dataItem.setNum(num);
        slimAdapterEx.notifyItemChanged(4);
    }

    @DealCodeClass(classes = {QiyeActivity.class})
    public void updateQiye(int num) {
        TipBean dataItem = (TipBean) slimAdapterEx.getDataItem(10);
        dataItem.setNum(num);
        slimAdapterEx.notifyItemChanged(10);
    }

    @Subscribe(code = CodeRule.USER_INFO, threadMode = ThreadMode.MAIN)
    public void updateUser() {
        user = GreenDaoUtil.getUser();
    }

    @Subscribe(code = 1, threadMode = ThreadMode.MAIN)
    public void updatePhoto(String photoUri) {
        ImageUtil.setCircleImage(photoUri, ivHead);
    }

    @Subscribe(code = 2, threadMode = ThreadMode.MAIN)
    public void updateNickname(String nickname) {
        tvName.setText(nickname);
    }

    @Subscribe(code = 3, threadMode = ThreadMode.MAIN)
    public void updateEQDCode(String eqdcode) {
        tvNum.setText("易企点号：" + eqdcode);
    }

    private int getCheckNum() {
        return GreenDaoUtil.getNum(CodeRule.CHECK_TRAVEL) + GreenDaoUtil.getNum(CodeRule.CHECK_SHIFT)
                + GreenDaoUtil.getNum(CodeRule.CHECK_OVER_TIME) + GreenDaoUtil.getNum(CodeRule.CHECK_OFF)
                + GreenDaoUtil.getNum(CodeRule.CHECK_LEAVE);
    }

    @Override
    protected void setView() {

        slimAdapterEx.getData().add(1);
        slimAdapterEx.getData().add(new TipBean(R.mipmap.shenq, "我的申请").setClazz(MyShenqingActivity.class));
        slimAdapterEx.getData().add(new TipBean(R.mipmap.pizhun, "我的批准").setClazz(MyPizhunActivity.class));
        slimAdapterEx.getData().add("1");
        slimAdapterEx.getData().add(new TipBean(R.mipmap.renwu, "我的任务").setClazz(RenwuActivity.class));
        slimAdapterEx.getData().add(new TipBean(R.mipmap.dak, "我的打卡").setClazz(DakaActivity.class));
        slimAdapterEx.getData().add(new TipBean(R.mipmap.kanb, "我的收藏").setClazz(CollectionActivity.class));
        slimAdapterEx.getData().add(new TipBean(R.mipmap.fuzerenproject, "备忘录").setClazz(ShowMemoActivity.class));
        slimAdapterEx.getData().add("1");

        slimAdapterEx.getData().add(new TipBean(R.mipmap.gere, "我").setClazz(WoActivity.class));
        slimAdapterEx.getData().add(new TipBean(R.mipmap.qiye, "我的企业").setClazz(QiyeActivity.class));
        slimAdapterEx.getData().add("1");

        slimAdapterEx.getData().add(new TipBean(R.mipmap.shezh, "系统设置").setClazz(XiTongSettingActivity.class));
        slimAdapterEx.getData().add("1");
        slimAdapterEx.notifyItemRangeChanged(0, slimAdapterEx.getData().size());
        if (dealCodeInjector!=null){
            ((MineFragment$$DealCodeInjector) dealCodeInjector).update1();
            ((MineFragment$$DealCodeInjector) dealCodeInjector).update2();
            ((MineFragment$$DealCodeInjector) dealCodeInjector).update3();
            ((MineFragment$$DealCodeInjector) dealCodeInjector).update4();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Config.USER_DOCUMENT) {
            user = GreenDaoUtil.getUser();
        }
    }

    @Override
    public void onDestroy() {
        RxBus.getDefault().unregister(this);
        DealCodeInjector.unregister(this, RxBus.class, dealCodeInjector);
        super.onDestroy();
    }


    @Override
    protected void initData() {
        dealCodeInjector = DealCodeInjector.register(this, RxBus.class);
        RxBus.getDefault().register(this);
        user = GreenDaoUtil.getUser();
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_15, new SlimInjector<TipBean>() {
                @Override
                public void onInject(TipBean data, IViewInjector injector) {
                    injector.text(R.id.tv_title, data.getTitle())
                            .image(R.id.iv_head, data.getResId());
                    if (data.getNum() > 0) {
                        injector.gone(R.id.drag_point_view);
                        injector.visible(R.id.red_dot);
                        injector.text(R.id.drag_point_view, data.getNum() + "");
                    } else {
                        injector.gone(R.id.drag_point_view);
                        injector.gone(R.id.red_dot);

                    }
                }
            })
                    .register(R.layout.list_item_32, new SlimInjector<String>() {
                        @Override
                        public void onInject(String data, IViewInjector injector) {

                        }
                    })
                    .registerDefault(R.layout.list_item_51, new SlimInjector() {
                        @Override
                        public void onInject(Object data, IViewInjector injector) {
                            injector.clicked(R.id.iv_erweima, v -> startActivity(UserErweimaActivity.class))
                                    .clicked(R.id.iv_head, v -> startActivity(UserInfoActivity.class))
                                    .imageCircle(R.id.iv_head, user.getIphoto())
                                    .text(R.id.tv_content, user.getPname())
                                    .text(R.id.tv_num, "易企点号：" + user.getEQDCode())
                                    .with(R.id.tv_content, view -> {
                                        tvName = (TextView) view;
                                    })
                                    .with(R.id.tv_num, view -> {
                                        tvNum = (TextView) view;
                                    })
                                    .with(R.id.iv_head, view -> {
                                        ivHead = (ImageView) view;
                                    });
                        }
                    }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener(
                            (rv, p, v) -> {
                                if (slimAdapterEx.getDataItem(p) instanceof Integer) {//头部
                                    new MaterialDialog.Builder(getActivity())
                                            .title("请输入登录密码")
                                            .inputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)//可以输入的类型-电话号码
                                            .input("请输入登陆密码", "", (dialog, input) -> Log.i("yqy", "输入的是：" + input))
                                            .onPositive((dialog, which) -> {
                                                if (dialog.getInputEditText().length() > 6) {
                                                    OkGo.<HttpResult<UserAuthInfoResultBean>>post(HttpConfig.BASE_URL + HttpConfig.GET_USER_ALL_INFO)
                                                            .params("userGuid", user.getGuid())
                                                            .params("password", EncryptUtil.sha1(dialog.getInputEditText().getText().toString() + "EQD"))
                                                            .execute(new DialogCallBack<HttpResult<UserAuthInfoResultBean>>((BaseActivity) getActivity()) {
                                                                @Override
                                                                public void onSuccess(Response<HttpResult<UserAuthInfoResultBean>> response) {
                                                                    HttpResult<UserAuthInfoResultBean> httpResult = response.body();
                                                                    if (httpResult.getStatus() == 200) {
                                                                        Bundle bundle = new Bundle();
                                                                        bundle.putSerializable(Config.USER_AUTH_INFO, httpResult.getItems());
                                                                        ((BaseActivity) getActivity()).startActivityForResult(UserDocumentActivity.class, bundle, Config.USER_DOCUMENT);
                                                                    } else if (httpResult.getStatus() == 202) {
                                                                        ToastUtil.showShort(httpResult.getMsg());
                                                                    } else if (httpResult.getStatus() == 500) {
                                                                        AlterDialogFragment alterDialogFragment = AlterDialogFragment.newInstance("提示", "你还未实名认证，是否实名认证", "取消", "确定");
                                                                        alterDialogFragment.setOnAlterDialogBtnListener(new AlterDialogFragment.AlterDialogBtnListener() {
                                                                            @Override
                                                                            public void onDialogPositiveClick(AlterDialogFragment alterDialogFragment) {
                                                                                startActivity(MineAuActivity.class);
                                                                            }

                                                                            @Override
                                                                            public void onDialogNegativeClick(AlterDialogFragment alterDialogFragment) {
                                                                                alterDialogFragment.dismiss();
                                                                            }
                                                                        });
                                                                        alterDialogFragment.show(getActivity().getSupportFragmentManager());
                                                                    }
                                                                }

                                                            });
                                                }
                                            })
                                            .show();
                                } else if (slimAdapterEx.getDataItem(p) instanceof TipBean) {
                                    TipBean dataItem = (TipBean) slimAdapterEx.getDataItem(p);
                                    startActivity(dataItem.getClazz());
                                }
                            }
                    );
        }
    }

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_mine, null, false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }
}
